//
//  NoContactCell.m
//  Guazi
//
//  Created by koolearn on 11-6-26.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "REString.h"
#import "StringUtil.h"
#import "NoContactCell.h"
#import "GuaziAppDelegate.h"
#import "NewContactViewController.h"

@interface NoContactCell(private)
-(void) send:(NSArray*)recipients body:(NSString*)body;
-(void)displaySMSComposerSheet:(NSArray*)recipients body:(NSString*)body;
@end


@implementation NoContactCell


@synthesize phoneButton;
@synthesize messageButton;
@synthesize addButton;
@synthesize phoneLabel;
@synthesize messageLabel;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.phoneLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        phoneLabel.font = [UIFont boldSystemFontOfSize:16];
        [self.contentView addSubview:phoneLabel];
        
        self.messageLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        messageLabel.font = [UIFont systemFontOfSize:13];
        messageLabel.textColor = [UIColor colorWithRed:0.4f green:0.4f blue:0.4f alpha:1.0f];
        messageLabel.lineBreakMode = UILineBreakModeWordWrap;
        messageLabel.numberOfLines = 3;
        messageLabel.text = @"查找的电话不存在。您可以拨打电话、发送短信，或者将该号码添加到通讯录中。";
        [self.contentView addSubview:messageLabel];
        
        self.phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        //[phoneButton setTitle:@"打电话" forState:UIControlStateNormal];
        phoneButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [phoneButton setBackgroundImage:[UIImage imageNamed:@"button_c_phone.png"] forState:UIControlStateNormal];
        [phoneButton addTarget:self action:@selector(callPhone) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:phoneButton];
        
        self.messageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        //[messageButton setTitle:@"发短信" forState:UIControlStateNormal];
        messageButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [messageButton setBackgroundImage:[UIImage imageNamed:@"button_c_message.png"] forState:UIControlStateNormal];
        [messageButton addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:messageButton];
        
        self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [addButton setTitle:@"加为联系人" forState:UIControlStateNormal];
        UIImage* image = [[UIImage imageNamed:@"button_green.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:0];
        [addButton setBackgroundImage:image forState:UIControlStateNormal];
        [addButton addTarget:self action:@selector(addContact) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:addButton];
    }
    return self;
}


- (void) layoutSubviews
{
    phoneLabel.frame = CGRectMake(10, 5, 200, 20);
    messageLabel.frame = CGRectMake(10, 25, 200, 60);
    phoneButton.frame = CGRectMake(210, 15, 45, 45);
    messageButton.frame = CGRectMake(260, 15, 45, 45);
    addButton.frame = CGRectMake(10, 90, 300, 35);
    [super layoutSubviews];
}


- (void) setPhone:(NSString*)phone
{
    self.phoneLabel.text = phone;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc
{
    [phoneLabel release];
    [messageLabel release];
    [phoneButton release];
    [messageButton release];
    [addButton release];
    [super dealloc];
}


//打电话
- (void) callPhone
{
	NSString* url = [NSString stringWithFormat:@"tel://%@", phoneLabel.text];
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}


//发短信
- (void) sendMessage
{
    NSArray* array = [NSArray arrayWithObject:phoneLabel.text];
    NSString* message = @"";
    [self send:array body:message];
}


//增加联系人
- (void) addContact
{
    NewContactViewController* controller = [[NewContactViewController alloc] init];
    
    NSString* s = [phoneLabel.text trim];
    NSMutableArray* array = [NSMutableArray array];

    BOOL isPhone = NO;
    if ( [s matches:@"^[0-9]+" withSubstring:array] ) {
        if ( [array count] > 0 ) {
            isPhone = YES;
        }
    }
    
    if ( isPhone ) {
        controller.phone = s;
    }
    else {
        controller.contactName = s;
    }
    
    [[[GuaziAppDelegate getAppDelegate] currentNavigationController] pushViewController:controller animated:YES];
    [controller release];
}

#pragma mark - SMS operations

-(void) send:(NSArray*)recipients body:(NSString*)body
{
	Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
	if (messageClass != nil) {
		// Check whether the current device is configured for sending SMS messages
		if ([messageClass canSendText]) {
			[self displaySMSComposerSheet:recipients body:body];
		}
		else {
			UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"设备没有短信功能" 
															message:@"您的设备不能发送短信" 
														   delegate:nil 
												  cancelButtonTitle:@"确定" 
												  otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	}
	else {
		UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"iOS版本过低" 
														message:@"iOS版本过低,iOS4.0以上才支持程序内发送短信" 
													   delegate:nil 
											  cancelButtonTitle:@"确定" 
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}


-(void)displaySMSComposerSheet:(NSArray*)recipients body:(NSString*)body
{
	MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
	picker.messageComposeDelegate = self;
	picker.recipients = recipients;
	picker.body = body;
    picker.title = @"发送短信";
    
	[[[GuaziAppDelegate getAppDelegate] currentNavigationController] presentModalViewController:picker animated:YES];
	[picker release];
}


- (void)messageComposeViewController:(MFMessageComposeViewController *)messageComposeViewController
				 didFinishWithResult:(MessageComposeResult)result {
	switch (result)
	{
		case MessageComposeResultCancelled:
			NSLog(@"Result: SMS sending canceled");
			break;
		case MessageComposeResultSent:
			NSLog(@"Result: SMS sent");
			break;
		case MessageComposeResultFailed:
			NSLog(@"Result: sms fail");
			UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"短信发送失败" 
															message:@"短信发送失败" 
														   delegate:nil 
												  cancelButtonTitle:@"确定" 
												  otherButtonTitles:nil];
			[alert show];
			[alert release];
			break;
		default:
			NSLog(@"Result: SMS not sent");
			break;
	}
    
    [[[GuaziAppDelegate getAppDelegate] currentNavigationController] dismissModalViewControllerAnimated:YES];
}


@end
